package com.leetcode.tree;

import java.util.*;

/**
 * 相似题目：637题 102题，其中637题还没做
 */
public class Leetcode107 {

    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int count = queue.size();
            List<Integer> inner = new ArrayList<>();
            while (count > 0) {
                TreeNode node = queue.poll();
                inner.add(node.val);
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
                count--;
            }
            list.add(inner);
        }
        Collections.reverse(list);
        return list;
    }
}
